package com.heatup.apt.api.service.impl;


import com.heatup.apt.api.service.CoachCustomerService;
import com.heatup.apt.api.service.CoachService;
import com.heatup.apt.common.constants.ErrorCodeConstants;
import com.heatup.apt.common.constants.OrderContants;
import com.heatup.apt.common.dao.CourseCoachMapper;
import com.heatup.apt.common.dao.OrderCourseCoachMapper;
import com.heatup.apt.common.dao.OrderInfoMapper;
import com.heatup.apt.common.dao.PrivOrderDetailResultModelMapper;
import com.heatup.apt.common.dao.coach.CoachInfoMapper;
import com.heatup.apt.common.util.StringUtil;
import com.heatup.apt.model.api.ApiResultModel;
import com.heatup.apt.model.coach.CoachBriefResultModel;
import com.heatup.apt.model.coach.CoachInfo;
import com.heatup.apt.model.course.CourseCoach;
import com.heatup.apt.model.order.OrderCourseCoach;
import com.heatup.apt.model.order.OrderInfo;
import com.heatup.apt.model.order.PrivOrderDetailResultModel;
import com.heatup.apt.model.user.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 *@Author: timo
 *@Date: 2018/3/22 11:15
 *@Description: 教练端-客户管理 实现类
 *
 *
 */
@Service
public class CoachCustomerServicelmpl implements CoachCustomerService{

    Logger logger = LoggerFactory.getLogger(CoachCustomerServicelmpl.class);
    @Resource
    private PrivOrderDetailResultModelMapper privOrderDetailResultModelMapper;
    @Resource
    private CoachService coachService;
    @Resource
    private OrderInfoMapper orderInfoMapper;
    @Resource
    private OrderCourseCoachMapper orderCourseCoachMapper;
    @Resource
    private CourseCoachMapper courseCoachMapper;
    @Resource
    private CoachInfoMapper coachInfoMapper;


    @Override
    public ApiResultModel queryCoachCustList(Map<String, Object> paramMap) {

        Integer orderType = (Integer) paramMap.get("type");
        UserInfo userInfo = (UserInfo) paramMap.get("userInfo");
        Integer sonCoachId = (Integer) paramMap.get("sonCoachId");
        Integer isChiefCoach = (Integer) paramMap.get("isChiefCoach");
        ApiResultModel<Object> model = new ApiResultModel<>();
        model.setRet(model.RET_ERROR);

        if (userInfo == null){
            model.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
            model.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
            model.setData(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
            return model;
        }

        if (StringUtil.isEmpty(userInfo.getMobile())){
            model.setMsg(ErrorCodeConstants.Code.COACH_MOBILE_NOT_BIND.getMessage());
            model.setErrorCode(ErrorCodeConstants.Code.COACH_MOBILE_NOT_BIND.getCode());
            model.setData(ErrorCodeConstants.Code.COACH_MOBILE_NOT_BIND.getMessage());
            return model;
        }

        // 查询教练
        CoachInfo coachInfo = null;
        if (sonCoachId > 0){
            coachInfo = coachInfoMapper.selectById(sonCoachId);
        }else{
            coachInfo = coachService.queryCoachByMobile(userInfo.getMobile());
        }
        if (coachInfo == null){
            model.setMsg(ErrorCodeConstants.Code.COACH_NOT_MATCH.getMessage());
            model.setErrorCode(ErrorCodeConstants.Code.COACH_NOT_MATCH.getCode());
            model.setData(ErrorCodeConstants.Code.COACH_NOT_MATCH.getMessage());
            return model;
        }



        if (orderType == 1){

            paramMap.put("orderType",9);
            paramMap.put("coachId",coachInfo.getId());

            // 查询体测体验课程订单列表
            List<PrivOrderDetailResultModel> plecOrderList = queryPlecOrderList(paramMap);

            // 查询私教订单数量
            paramMap.put("orderType",2);
            Integer privOrderCount = privOrderDetailResultModelMapper.queryPrivOrderListCountBy(paramMap);
            paramMap.clear();

            paramMap.put("orderCount",plecOrderList.size());
            paramMap.put("plecOrderCount",plecOrderList.size());
            paramMap.put("privOrderCount",privOrderCount);
            paramMap.put("orderList",plecOrderList);
        }else{

            paramMap.put("orderType",2);
            paramMap.put("coachId",coachInfo.getId());
            // 查询私教订单
            List<PrivOrderDetailResultModel> privOrderList = queryPrivateOrderList(paramMap);

            // 查询体测体验课程的订单数量
            paramMap.put("orderType",9);
            Integer plecOrderCount = privOrderDetailResultModelMapper.queryPrivOrderListCountBy(paramMap);
            paramMap.clear();

            paramMap.put("orderCount",privOrderList.size());
            paramMap.put("plecOrderCount",plecOrderCount);
            paramMap.put("privOrderCount",privOrderList.size());
            paramMap.put("orderList",privOrderList);

        }
        paramMap.put("isChiefCoach",isChiefCoach);
        model.setData(paramMap);
        model.setRet(model.RET_SUCCESS);
        return model;
    }

    public List<PrivOrderDetailResultModel> queryPlecOrderList(Map<String,Object> paramMap){
        List<PrivOrderDetailResultModel> plecOrderList = privOrderDetailResultModelMapper.queryPrivOrderListBy(paramMap);
        return plecOrderList;
    }

    public List<PrivOrderDetailResultModel> queryPrivateOrderList(Map<String,Object> paramMap){

        List<PrivOrderDetailResultModel> privOrderList = privOrderDetailResultModelMapper.queryPrivOrderListBy(paramMap);
        return privOrderList;
    }

    /**
     *@Author: timo
     *@Date: 2018/3/22 20:07 
     *@Description:
     * 查询订单详情
     *
     */
    @Override
    public ApiResultModel queryOrderDetail(UserInfo userInfo, Integer orderId) {
        CoachInfo coachInfo = coachService.queryCoachByMobile(userInfo.getMobile());

        return null;
    }

    /**
     *@Author: timo
     *@Date: 2018/3/28 14:44
     *@Description:
     * 修改教练
     * 上课的教练只能是增加不能减少
     * 1.订单课程教练表需要修稿
     * 2.课程教练表需要修改
     * 3.只能是教练主管才能修改
     */
    @Override
    public ApiResultModel updateCoach(Map<String,Object> paramMap) {
        ApiResultModel<Object> resultModel = new ApiResultModel<>();
        UserInfo userInfo = (UserInfo) paramMap.get("userInfo");
        Integer orderId = (Integer) paramMap.get("orderId");
        Integer sonCoachId = (Integer) paramMap.get("sonCoachId");
        List<Integer> coachIdList = (List<Integer>) paramMap.get("coachIdList");

        // step 1 判断是不是主教练 不是主教练就没有修改教练的权限
        CoachInfo coachInfo = coachService.queryCoachByMobile(userInfo.getMobile());

        if (coachInfo == null){
            resultModel.setErrorCode(ErrorCodeConstants.Code.COACH_NOT_MATCH.getCode());
            resultModel.setData(ErrorCodeConstants.Code.COACH_NOT_MATCH.getMessage());
            resultModel.setMsg(ErrorCodeConstants.Code.COACH_NOT_MATCH.getMessage());
            return resultModel;
        }

        if (coachInfo.getIsChiefCoach() != 1 ){
            resultModel.setErrorCode(ErrorCodeConstants.Code.NOT_HAS_PERMISSIONS.getCode());
            resultModel.setData(ErrorCodeConstants.Code.NOT_HAS_PERMISSIONS.getMessage());
            resultModel.setMsg(ErrorCodeConstants.Code.NOT_HAS_PERMISSIONS.getMessage());
            return resultModel;
        }

        // step 2 校验订单是否是私教订单
        OrderInfo orderInfo = orderInfoMapper.selectById(orderId);
        if(orderInfo ==null){
            resultModel.setRet(resultModel.RET_ERROR);
            resultModel.setMsg(ErrorCodeConstants.Code.ORDER_NOT_FIND.getMessage());
            resultModel.setErrorCode(ErrorCodeConstants.Code.ORDER_NOT_FIND.getCode());
            resultModel.setData("更新失败");
            return resultModel;
        }
        if(OrderContants.OrderType.pt.getIndex() != orderInfo.getOrderType()){
            resultModel.setRet(resultModel.RET_ERROR);
            resultModel.setMsg("该订单不是私教订单");
            return  resultModel;
        }

        int addCount = createOrderCourseCoach(orderId,coachIdList,userInfo);
        if (addCount <= 0){
            resultModel.setRet(resultModel.RET_ERROR);
            resultModel.setMsg("更新失败");
            resultModel.setData("更新失败");
            return resultModel;
        }

        resultModel.setRet(resultModel.RET_SUCCESS);
        resultModel.setMsg("更新成功");
        resultModel.setData("更新成功！");
        return resultModel;
    }

    /**
     *@Author: timo
     *@Date: 2018/3/28 17:34
     *@Description:
     * 查询订单教练列表
     *
     */
    @Override
    public Map<String, Object> queryOrderCoachList(UserInfo userInfo, Integer orderId) {
        Map<String,Object> resultMap = new HashMap<>();
        CoachInfo coachInfo = coachService.queryCoachByMobile(userInfo.getMobile());
        if (coachInfo !=null){
            List<CoachBriefResultModel> coachList = orderCourseCoachMapper.queryCoachTeamByOrderId(orderId,userInfo.getMobile());
            resultMap.put("coachList",coachList);
            return resultMap;
        }
        return null;
    }

    /**
     *@Author: timo
     *@Date: 2018/3/29 23:07
     *@Description:
     * 教练端订单取消
     * 1.只能取消体测体验的订单 暂时只是开放这两种订单给教练使用
     *
     */
    @Override
    public ApiResultModel coachCancelOrder(UserInfo userInfo, Integer orderId) {

        ApiResultModel<Object> resultModel = new ApiResultModel<>();

        // step 1 判断是不是主教练 不是主教练就没有修改教练的权限
        CoachInfo coachInfo = coachService.queryCoachByMobile(userInfo.getMobile());
        if (coachInfo == null){
            resultModel.setErrorCode(ErrorCodeConstants.Code.COACH_NOT_MATCH.getCode());
            resultModel.setData(ErrorCodeConstants.Code.COACH_NOT_MATCH.getMessage());
            resultModel.setMsg(ErrorCodeConstants.Code.COACH_NOT_MATCH.getMessage());
            return resultModel;
        }

        OrderInfo orderInfo = orderInfoMapper.selectById(orderId);
        if (orderInfo == null){
            resultModel.setRet(resultModel.RET_ERROR);
            resultModel.setErrorCode(ErrorCodeConstants.Code.ORDER_NOT_FIND.getCode());
            resultModel.setData(ErrorCodeConstants.Code.ORDER_NOT_FIND.getMessage());
            resultModel.setMsg(ErrorCodeConstants.Code.ORDER_NOT_FIND.getMessage());
            return resultModel;
        }

        if(orderInfo.getOrderType() != OrderContants.OrderType.physical_examination.getIndex()
                && orderInfo.getOrderType() != OrderContants.OrderType.secret_experience.getIndex()){
            resultModel.setRet(resultModel.RET_ERROR);
            resultModel.setMsg("该订单不是体测体验课订单不能取消！");
            return resultModel;
        }

        // 修改订单
        orderInfo.setOrderStatus(OrderContants.OrderStatus.CANCEL.getStatus());
        orderInfo.setCancel(2);
        orderInfo.setCancelBackmoney(1);
        orderInfo.setUpdatedTime(new Date());
        int updateCount = orderInfoMapper.updateById(orderInfo);
        if (updateCount > 0){
            resultModel.setRet(resultModel.RET_SUCCESS);
            resultModel.setMsg("取消成功");
            return resultModel;
        }else {
            resultModel.setRet(resultModel.RET_ERROR);
            resultModel.setMsg("取消成功");
            return resultModel;
        }
    }


    /**
     *@Author: timo
     *@Date: 2018/3/28 15:35
     *@Description:
     * 添加订单课程教练详情
     *
     */
    public int createOrderCourseCoach(Integer orderId, List<Integer> coachIds, UserInfo userInfo){
        List<OrderCourseCoach> orderCoachList = new ArrayList<>();
        // 查找出旧的教练
        List<OrderCourseCoach> orderCoachListOld = orderCourseCoachMapper.queryOrderCourseCoachByOrderId(orderId);
        // 教练已存在的教练
        List<Integer> idsOld = new ArrayList<>();
        for(OrderCourseCoach orderCoach : orderCoachListOld){
            idsOld.add(orderCoach.getCoachId());
        }
        coachIds.removeAll(idsOld);

        // 添加新教练
        if(orderCoachListOld.size() > 0){
            OrderCourseCoach orderCourseCoach = orderCoachListOld.get(0);
            for (Integer id : coachIds){
                OrderCourseCoach occ = new OrderCourseCoach();
                occ.setCourseId(orderCourseCoach.getCourseId());
                occ.setCourseName(orderCourseCoach.getCourseName());
                occ.setCoachId(id);
                occ.setOrderId(orderCourseCoach.getOrderId());
                occ.setOrderType(orderCourseCoach.getOrderType());
                occ.setCreateTime(new Date());
                occ.setCreateBy(userInfo.getId());
                orderCoachList.add(occ);
            }

            // 修改教练
            if (orderCoachList.size() > 0) {
                int addCount = orderCourseCoachMapper.insertBatch(orderCoachList);
                if (addCount > 0) return createCourseCoach(orderCourseCoach.getCourseId(),coachIds,userInfo);
            }

        }

        return 0;
    }

    /**
     *@Author: timo
     *@Date: 2018/3/28 15:35
     *@Description:
     * 添加课程教练关系详情
     *
     */
    public int createCourseCoach(Integer courseId,List<Integer> coachIds, UserInfo userInfo){
        List<CourseCoach> courseCoachList = new ArrayList<>();
        for (Integer id : coachIds){
            CourseCoach cc = new CourseCoach();
            cc.setCourseId(courseId);
            cc.setCoachId(id);
            cc.setStatus("1");
            cc.setCreatedBy(userInfo.getCreatedBy());
            cc.setCreatedTime(new Date());
            cc.setVersion(1);
            courseCoachList.add(cc);
        }

        if (courseCoachList.size() > 0) return courseCoachMapper.insertBatch(courseCoachList);

        return 0;
    }


}
